package com.example.apple.hxbtc.view;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.apple.hxbtc.R;


public class View_Dialog extends Dialog implements
        View.OnClickListener {

    private LinearLayout layoutContainer;
    private TextView btnOK;
    private TextView dialog_text, dialog_title, phone_text;
    private TextView btnCancle;
    private EditText dialog_description_et;
    private RadioGroup satisfiedRadio;

    private boolean isCanExist;
    private String isSatisfied = "1";

    public View_Dialog(Context context, boolean isCancle) {
        super(context, R.style.dialog);
        setContentView(R.layout.dialog);
        btnOK = (TextView) findViewById(R.id.dialog_ok);
        layoutContainer = (LinearLayout) findViewById(R.id.dialog_container);
        layoutContainer.setVisibility(View.GONE);
        btnCancle = (TextView) findViewById(R.id.dialog_cancle);
        btnCancle.setVisibility(isCancle ? View.VISIBLE : View.GONE);
        dialog_title = (TextView) findViewById(R.id.dialog_title);
        dialog_title.setText("温馨提示");
        dialog_text = (TextView) findViewById(R.id.dialog_text);
        btnOK.setOnClickListener(this);
        btnCancle.setOnClickListener(this);
        setCanceledOnTouchOutside(true);
    }

    public View_Dialog(Context context, String title, String content, int layoutVisible) {
        super(context, R.style.dialog);
        setContentView(R.layout.dialog);
        btnOK = (TextView) findViewById(R.id.dialog_ok);
        layoutContainer = (LinearLayout) findViewById(R.id.dialog_container);
        layoutContainer.setVisibility(layoutVisible);
        satisfiedRadio = (RadioGroup) this.findViewById(R.id.radioGroup);
        dialog_description_et = (EditText) findViewById(R.id.dialog_description_et);
        btnCancle = (TextView) findViewById(R.id.dialog_cancle);
        btnCancle.setVisibility(View.VISIBLE);
        dialog_title = (TextView) findViewById(R.id.dialog_title);
        dialog_title.setText(title);
        dialog_text = (TextView) findViewById(R.id.dialog_text);
        dialog_text.setText(content);

        satisfiedRadio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int radioButtonId = group.getCheckedRadioButtonId();
                RadioButton rb = (RadioButton) findViewById(radioButtonId);
                if (rb.getContentDescription().equals("满意"))
                    isSatisfied = "1";
                else isSatisfied = "0";
                //Logger.e("checkid is" + isSatisfied);
            }
        });
        btnOK.setOnClickListener(this);
        btnCancle.setOnClickListener(this);
        setCanceledOnTouchOutside(true);
    }


    public View_Dialog(Context context, String content) {
        super(context, R.style.dialog);
        setContentView(R.layout.dialog);
        btnOK = (TextView) findViewById(R.id.dialog_ok);
        btnCancle = (TextView) findViewById(R.id.dialog_cancle);
        btnCancle.setVisibility(View.VISIBLE);
        layoutContainer = (LinearLayout) findViewById(R.id.dialog_container);
        layoutContainer.setVisibility(View.GONE);
        dialog_title = (TextView) findViewById(R.id.dialog_title);
        dialog_title.setText(content);
        dialog_text = (TextView) findViewById(R.id.dialog_text);
        dialog_text.setVisibility(View.GONE);
        phone_text = (TextView) findViewById(R.id.phone_text);
        phone_text.setVisibility(View.VISIBLE);

        btnOK.setOnClickListener(this);
        btnCancle.setOnClickListener(this);
        setCanceledOnTouchOutside(true);
    }

    public View_Dialog(Context context, boolean isCancle, String title) {
        super(context, R.style.dialog);
        setContentView(R.layout.dialog);
        isCanExist = isCancle;
        btnOK = (TextView) findViewById(R.id.dialog_ok);
        layoutContainer = (LinearLayout) findViewById(R.id.dialog_container);
        layoutContainer.setVisibility(View.GONE);
        btnCancle = (TextView) findViewById(R.id.dialog_cancle);
        btnCancle.setVisibility(isCancle ? View.VISIBLE : View.GONE);
        dialog_title = (TextView) findViewById(R.id.dialog_title);
        dialog_title.setText(title);
        dialog_text = (TextView) findViewById(R.id.dialog_text);
        btnOK.setOnClickListener(this);
        btnCancle.setOnClickListener(this);
        setCanceledOnTouchOutside(false);
        setCancelable(isCancle);
    }


    public void setContentText(int resID) {
        dialog_text.setText(getContext().getString(resID));
    }

    public void setContentText(String mtitle) {
        dialog_text.setText(mtitle);
    }

    public void setContainerLayout(View view) {
        layoutContainer.removeAllViews();
        if (view.getParent() != null) {
            ((ViewGroup) view.getParent()).removeView(view);
        }
        layoutContainer.addView(view);
    }

    public void setContainerLayout(int resID) {
        setContainerLayout(LayoutInflater.from(getContext()).inflate(resID,
                null));
    }

    public abstract interface DialogCallback {
        void doClocseQuestion(String isSatisfied, String description);
    }

    private DialogCallback callback;

    private View.OnClickListener leftBtnListener;
    private View.OnClickListener rightBtnListener;

    public void setLeftBtnListener(String text,
                                   View.OnClickListener onClickListener) {
        btnCancle.setText(text);
        leftBtnListener = onClickListener;

    }

    public void setLeftBtnListener(int textID,
                                   View.OnClickListener onClickListener) {
        btnCancle.setText(getContext().getString(textID));
        leftBtnListener = onClickListener;
    }

    public void setRightBtnListener(String text,
                                    View.OnClickListener onClickListener) {
        btnOK.setText(text);
        rightBtnListener = onClickListener;
    }


    public void setRightBtnListener(String text,
                                    View.OnClickListener onClickListener, DialogCallback callBack) {
        btnOK.setText(text);
        rightBtnListener = onClickListener;
        callback = callBack;
    }

    public void setRightBtnListener(int textID,
                                    View.OnClickListener onClickListener) {
        btnOK.setText(getContext().getString(textID));
        rightBtnListener = onClickListener;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_cancle:
                if (leftBtnListener != null)
                    leftBtnListener.onClick(v);
                break;
            case R.id.dialog_ok:
                if (rightBtnListener != null)
                    rightBtnListener.onClick(v);
                if (callback != null)
                    callback.doClocseQuestion(isSatisfied, dialog_description_et.getText().toString());
                break;

        }
        if (isCanExist)
            dismiss();
    }

}
